{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "27db5cb8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "求解状态: Optimal\n",
      "最小总成本: $2320.00\n",
      "\n",
      "最优运输方案:\n",
      "Factory1 -> Warehouse1: 80.0 单位\n",
      "Factory1 -> Warehouse3: 40.0 单位\n",
      "Factory2 -> Warehouse2: 60.0 单位\n",
      "Factory2 -> Warehouse4: 80.0 单位\n",
      "Factory3 -> Warehouse2: 30.0 单位\n",
      "Factory3 -> Warehouse3: 70.0 单位\n",
      "\n",
      "工厂使用情况:\n",
      "Factory1: 供应 120.0/120 单位\n",
      "Factory2: 供应 140.0/140 单位\n",
      "Factory3: 供应 100.0/100 单位\n",
      "\n",
      "仓库需求满足情况:\n",
      "Warehouse1: 接收 80.0/80 单位\n",
      "Warehouse2: 接收 90.0/90 单位\n",
      "Warehouse3: 接收 110.0/110 单位\n",
      "Warehouse4: 接收 80.0/80 单位\n"
     ]
    }
   ],
   "source": [
    "import pulp\n",
    "\n",
    "# 创建最小化问题\n",
    "problem = pulp.LpProblem(\"Transportation_Problem\", pulp.LpMinimize)\n",
    "\n",
    "# 定义数据\n",
    "supply_points = [\"Factory1\", \"Factory2\", \"Factory3\"]\n",
    "supply = {\"Factory1\": 120, \"Factory2\": 140, \"Factory3\":100}\n",
    "\n",
    "demand_points = [\"Warehouse1\", \"Warehouse2\", \"Warehouse3\",\"Warehouse4\"]\n",
    "demand = {\"Warehouse1\": 80, \"Warehouse2\": 90, \"Warehouse3\": 110,\"Warehouse4\":80}\n",
    "\n",
    "costs = {\n",
    "    \"Factory1\": {\"Warehouse1\": 5, \"Warehouse2\": 7, \"Warehouse3\": 9,\"Warehouse4\":6},\n",
    "     \"Factory2\": {\"Warehouse1\": 6, \"Warehouse2\": 7, \"Warehouse3\": 10,\"Warehouse4\":5},\n",
    "      \"Factory3\": {\"Warehouse1\": 7, \"Warehouse2\": 6, \"Warehouse3\": 8,\"Warehouse4\":9}\n",
    "}\n",
    "\n",
    "# 创建决策变量\n",
    "x = pulp.LpVariable.dicts(\"Route\", (supply_points, demand_points), lowBound=0, cat=\"Continuous\")\n",
    "\n",
    "# 目标函数：最小化总运输成本\n",
    "problem += pulp.lpSum([x[i][j] * costs[i][j] for i in supply_points for j in demand_points])\n",
    "\n",
    "# 供应约束\n",
    "for i in supply_points:\n",
    "    problem += pulp.lpSum([x[i][j] for j in demand_points]) <= supply[i]\n",
    "\n",
    "# 需求约束\n",
    "for j in demand_points:\n",
    "    problem += pulp.lpSum([x[i][j] for i in supply_points]) >= demand[j]\n",
    "\n",
    "# 求解问题\n",
    "problem.solve()\n",
    "\n",
    "# 输出结果\n",
    "print(f\"求解状态: {pulp.LpStatus[problem.status]}\")\n",
    "print(f\"最小总成本: ${pulp.value(problem.objective):.2f}\\n\")\n",
    "\n",
    "print(\"最优运输方案:\")\n",
    "for i in supply_points:\n",
    "    for j in demand_points:\n",
    "        if x[i][j].value() > 0:\n",
    "            print(f\"{i} -> {j}: {x[i][j].value()} 单位\")\n",
    "\n",
    "# 输出各工厂使用情况\n",
    "print(\"\\n工厂使用情况:\")\n",
    "for i in supply_points:\n",
    "    total_supply = sum(x[i][j].value() for j in demand_points)\n",
    "    print(f\"{i}: 供应 {total_supply}/{supply[i]} 单位\")\n",
    "\n",
    "# 输出各仓库满足情况\n",
    "print(\"\\n仓库需求满足情况:\")\n",
    "for j in demand_points:\n",
    "    total_demand = sum(x[i][j].value() for i in supply_points)\n",
    "    print(f\"{j}: 接收 {total_demand}/{demand[j]} 单位\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "math_model",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
